package controllers

import (
	"finance/internal/models"
	"finance/internal/utils"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

type MenuItem struct {
	Name string `json:"name"`
	Path string `json:"path"`
}

func GetMyMenus(c *gin.Context) {
	roleID := c.GetUint("roleID")

	fmt.Println("get menus: ", roleID)
	if roleID == 0 {
		utils.Fail(c, http.StatusForbidden, "Role not found in token")
		return
	}

	permissions, err := models.GetPagePermissionsByRoleID(roleID)
	if err != nil {
		utils.Fail(c, http.StatusInternalServerError, "Failed to get permissions")
		return
	}

	menus := make([]MenuItem, 0, len(permissions))
	for _, p := range permissions {
		menus = append(menus, MenuItem{
			Name: p.Name,
			Path: p.Path,
		})
	}

	utils.Success(c, menus)
}
